/**
 * 验证所有160个等级的坐骑经验数据脚本
 */

const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: '43.143.253.188',
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

// 根据表格的完整数据 - 这里只包含关键等级，其他等级按规律推算
const keyLevels = {
  1: { expToNext: 100, expPerTraining: 7500, trainingCount: 1, totalTraining: 0 },
  2: { expToNext: 200, expPerTraining: 7500, trainingCount: 1, totalTraining: 1 },
  3: { expToNext: 300, expPerTraining: 7500, trainingCount: 1, totalTraining: 2 },
  4: { expToNext: 400, expPerTraining: 7500, trainingCount: 1, totalTraining: 3 },
  5: { expToNext: 500, expPerTraining: 7500, trainingCount: 1, totalTraining: 4 },
  6: { expToNext: 600, expPerTraining: 7500, trainingCount: 1, totalTraining: 5 },
  7: { expToNext: 700, expPerTraining: 7500, trainingCount: 1, totalTraining: 6 },
  8: { expToNext: 800, expPerTraining: 7500, trainingCount: 1, totalTraining: 7 },
  9: { expToNext: 900, expPerTraining: 7500, trainingCount: 1, totalTraining: 8 },
  10: { expToNext: 1450, expPerTraining: 7500, trainingCount: 1, totalTraining: 9 },
  11: { expToNext: 2145, expPerTraining: 7800, trainingCount: 1, totalTraining: 10 },
  12: { expToNext: 3240, expPerTraining: 8100, trainingCount: 1, totalTraining: 11 },
  13: { expToNext: 4125, expPerTraining: 8250, trainingCount: 1, totalTraining: 12 },
  14: { expToNext: 5130, expPerTraining: 8550, trainingCount: 1, totalTraining: 13 },
  15: { expToNext: 6090, expPerTraining: 8700, trainingCount: 1, totalTraining: 14 },
  16: { expToNext: 7200, expPerTraining: 9000, trainingCount: 1, totalTraining: 15 },
  17: { expToNext: 8235, expPerTraining: 9150, trainingCount: 1, totalTraining: 16 },
  18: { expToNext: 9450, expPerTraining: 9450, trainingCount: 1, totalTraining: 17 },
  19: { expToNext: 10560, expPerTraining: 9600, trainingCount: 1, totalTraining: 18 },
  20: { expToNext: 11880, expPerTraining: 9900, trainingCount: 1, totalTraining: 19 },
  21: { expToNext: 13050, expPerTraining: 10050, trainingCount: 1, totalTraining: 20 },
  22: { expToNext: 14400, expPerTraining: 10350, trainingCount: 2, totalTraining: 21 },
  23: { expToNext: 15750, expPerTraining: 10500, trainingCount: 1, totalTraining: 23 },
  24: { expToNext: 17280, expPerTraining: 10800, trainingCount: 1, totalTraining: 24 },
  25: { expToNext: 18675, expPerTraining: 10950, trainingCount: 3, totalTraining: 25 },
  26: { expToNext: 20250, expPerTraining: 11250, trainingCount: 1, totalTraining: 28 },
  27: { expToNext: 21600, expPerTraining: 11400, trainingCount: 1, totalTraining: 29 },
  28: { expToNext: 23400, expPerTraining: 11700, trainingCount: 1, totalTraining: 30 },
  29: { expToNext: 24840, expPerTraining: 11850, trainingCount: 1, totalTraining: 31 },
  30: { expToNext: 26730, expPerTraining: 12150, trainingCount: 1, totalTraining: 32 },
  31: { expToNext: 28290, expPerTraining: 12300, trainingCount: 1, totalTraining: 33 },
  32: { expToNext: 30240, expPerTraining: 12600, trainingCount: 1, totalTraining: 34 },
  33: { expToNext: 31875, expPerTraining: 12750, trainingCount: 1, totalTraining: 35 },
  34: { expToNext: 33930, expPerTraining: 13050, trainingCount: 1, totalTraining: 36 },
  35: { expToNext: 35640, expPerTraining: 13200, trainingCount: 1, totalTraining: 37 },
  36: { expToNext: 37800, expPerTraining: 13500, trainingCount: 1, totalTraining: 38 },
  37: { expToNext: 39510, expPerTraining: 13950, trainingCount: 1, totalTraining: 39 },
  38: { expToNext: 41760, expPerTraining: 14400, trainingCount: 1, totalTraining: 40 },
  39: { expToNext: 43560, expPerTraining: 14850, trainingCount: 1, totalTraining: 41 },
  40: { expToNext: 45900, expPerTraining: 15300, trainingCount: 6, totalTraining: 42 },
  100: { expToNext: 4499227, expPerTraining: 75600, trainingCount: 59, totalTraining: 1147 },
  128: { expToNext: 6137227, expPerTraining: 84000, trainingCount: 169, totalTraining: 1233 },
  160: { expToNext: 76138792, expPerTraining: 133200, trainingCount: 572, totalTraining: 14028 }
};

async function verifyAllLevels () {
  let connection;

  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');

    console.log('🔍 开始验证所有160个等级的坐骑经验数据...\n');

    // 获取数据库中的所有等级数据
    const [dbRows] = await connection.execute(
      'SELECT level, experience, exp_to_next, exp_per_training, training_count, total_training FROM mount_level_experience ORDER BY level'
    );

    let totalLevels = 0;
    let matchedLevels = 0;
    let mismatchedLevels = 0;
    const mismatches = [];

    console.log('📊 关键等级验证结果:');
    console.log('等级\t表格升级经验\t数据库升级经验\t表格训练经验\t数据库训练经验\t状态');
    console.log('----\t------------\t------------\t------------\t------------\t----');

    // 验证关键等级
    for (const dbRow of dbRows) {
      const level = dbRow.level;
      const tableDataForLevel = keyLevels[level];

      if (tableDataForLevel) {
        totalLevels++;

        const expToNextMatch = dbRow.exp_to_next === tableDataForLevel.expToNext;
        const expPerTrainingMatch = dbRow.exp_per_training === tableDataForLevel.expPerTraining;
        const trainingCountMatch = dbRow.training_count === tableDataForLevel.trainingCount;
        const totalTrainingMatch = dbRow.total_training === tableDataForLevel.totalTraining;

        const allMatch = expToNextMatch && expPerTrainingMatch && trainingCountMatch && totalTrainingMatch;

        if (allMatch) {
          matchedLevels++;
          console.log(`${level}\t${tableDataForLevel.expToNext}\t\t${dbRow.exp_to_next}\t\t${tableDataForLevel.expPerTraining}\t\t${dbRow.exp_per_training}\t\t✅`);
        } else {
          mismatchedLevels++;
          mismatches.push({
            level,
            table: tableDataForLevel,
            database: dbRow,
            issues: []
          });

          if (!expToNextMatch) mismatches[mismatches.length - 1].issues.push(`升级经验: 表格${tableDataForLevel.expToNext} vs 数据库${dbRow.exp_to_next}`);
          if (!expPerTrainingMatch) mismatches[mismatches.length - 1].issues.push(`训练经验: 表格${tableDataForLevel.expPerTraining} vs 数据库${dbRow.exp_per_training}`);
          if (!trainingCountMatch) mismatches[mismatches.length - 1].issues.push(`训练次数: 表格${tableDataForLevel.trainingCount} vs 数据库${dbRow.training_count}`);
          if (!totalTrainingMatch) mismatches[mismatches.length - 1].issues.push(`总训练次数: 表格${tableDataForLevel.totalTraining} vs 数据库${dbRow.total_training}`);

          console.log(`${level}\t${tableDataForLevel.expToNext}\t\t${dbRow.exp_to_next}\t\t${tableDataForLevel.expPerTraining}\t\t${dbRow.exp_per_training}\t\t❌`);
        }
      }
    }

    console.log('\n📈 关键等级验证统计:');
    console.log(`总关键等级数: ${totalLevels}`);
    console.log(`匹配等级数: ${matchedLevels}`);
    console.log(`不匹配等级数: ${mismatchedLevels}`);
    console.log(`匹配率: ${((matchedLevels / totalLevels) * 100).toFixed(2)}%`);

    // 检查训练经验值的递增规律
    console.log('\n🔍 检查训练经验值递增规律:');
    const trainingExpValues = dbRows.map(row => row.exp_per_training);
    let consistentIncrement = true;

    for (let i = 1; i < trainingExpValues.length; i++) {
      if (trainingExpValues[i] < trainingExpValues[i - 1]) {
        console.log(`❌ 等级${i + 1}的训练经验(${trainingExpValues[i]})小于等级${i}的训练经验(${trainingExpValues[i - 1]})`);
        consistentIncrement = false;
      }
    }

    if (consistentIncrement) {
      console.log('✅ 训练经验值递增规律正确');
    }

    // 检查特殊等级的训练次数
    console.log('\n🔍 检查特殊等级的训练次数:');
    const specialLevels = [22, 25, 40, 100, 128, 160];
    specialLevels.forEach(level => {
      const dbLevel = dbRows.find(row => row.level === level);
      if (dbLevel) {
        console.log(`等级${level}: 训练次数=${dbLevel.training_count}, 总训练次数=${dbLevel.total_training}`);
      }
    });

    if (mismatches.length > 0) {
      console.log('\n❌ 不匹配详情:');
      mismatches.forEach(mismatch => {
        console.log(`\n等级 ${mismatch.level}:`);
        mismatch.issues.forEach(issue => {
          console.log(`  - ${issue}`);
        });
      });
    }

    console.log('\n🎉 所有等级验证完成！');

  } catch (error) {
    console.error('❌ 验证失败:', error.message);
    throw error;
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

// 运行脚本
if (require.main === module) {
  verifyAllLevels().catch((error) => {
    console.error('💥 脚本执行失败:', error.message);
    process.exit(1);
  });
}

module.exports = { verifyAllLevels };
